package com.xczz.controller;

import com.xczz.common.constant.Constants;
import com.xczz.common.result.RestResult;
import com.xczz.common.security.LoginUser;
import com.xczz.common.security.service.SysLoginService;
import com.xczz.common.security.service.SysPermissionService;
import com.xczz.common.security.service.TokenService;
import com.xczz.model.vo.SysUserVO;
import com.xczz.utils.http.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysPermissionService permissionService;
    @PostMapping({"/login","/"})
    public RestResult login(String username,String password,String uuid){
        RestResult ajax=RestResult.success();
        String token=loginService.login(username,password,"123",uuid);
        ajax.put(Constants.TOKEN,token);
        return ajax;
    }

    @GetMapping("getInfo")
    public RestResult getInfo(){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUserVO user=loginUser.getUser();
        Set<String> roles=permissionService.getRolePermission(user);
        Set<String> permissions = permissionService.getMenuPermission(user);
        RestResult ajax= RestResult.success();
        ajax.put("user",user);
        ajax.put("roles",roles);
        ajax.put("permissions",permissions);
        return ajax;
    }
}
